import cv2
import numpy as np

# 初始化为
TRANSLATION = 0
AFFINE = 1
pyramid_level = 1
imp = None
stack = None
stackOut = None
outputDir = None
stackVirtual = False
outputNewStack = False
transform = TRANSLATION
pyramidLevel = 1
maxIter = 200
tol = 1e-7
alpha = 0.9

def process(image_sequence, first_slice, last_slice, interval):
    stack_size = len(image_sequence)
    width, height, _ = image_sequence[0].shape

    ip_float_ref = None

    # 创建金字塔
    ip_pyramid = [None] * 5
    ip_ref_pyramid = [None] * 5

    ip_pyramid[0] = np.zeros((width, height), dtype=np.float32)
    ip_ref_pyramid[0] = np.zeros((width, height), dtype=np.float32)

    # 创建不同层级的金字塔
    if pyramid_level >= 1 and width >= 100 and height >= 100:
        width2 = width // 2
        height2 = height // 2
        ip_pyramid[1] = np.zeros((width2, height2), dtype=np.float32)
        ip_ref_pyramid[1] = np.zeros((width2, height2), dtype=np.float32)
        if pyramid_level >= 2 and width >= 200 and height >= 200:
            width4 = width // 4
            height4 = height // 4
            ip_pyramid[2] = np.zeros((width4, height4), dtype=np.float32)
            ip_ref_pyramid[2] = np.zeros((width4, height4), dtype=np.float32)
            if pyramid_level >= 3 and width >= 400 and height >= 400:
                width8 = width // 8
                height8 = height // 8
                ip_pyramid[3] = np.zeros((width8, height8), dtype=np.float32)
                ip_ref_pyramid[3] = np.zeros((width8, height8), dtype=np.float32)
                if pyramid_level >= 4 and width >= 800 and height >= 800:
                    width16 = width // 16
                    height16 = height // 16
                    ip_pyramid[4] = np.zeros((width16, height16), dtype=np.float32)
                    ip_ref_pyramid[4] = np.zeros((width16, height16), dtype=np.float32)

    for slice in range(first_slice, last_slice + 1, interval):
        # 如果用户按下了ESC键或者图像窗口已关闭，则停止处理
        if cv2.waitKey(1) & 0xFF == 27:
            break

        label = str(slice)

        if slice == first_slice and interval > 0:
            # 跳过处理第一帧的过程，直接保存或输出原图像
            print("Skipping " + str(slice) + "/" + str(stack_size) + " ...")

            if stack_out is not None:
                stack_out.append(image_sequence[slice])
            else:
                # 处理原图像
                pass

        else:
            print("Stabilizing " + str(slice) + "/" + str(stack_size) + " ...")

            # 处理其他帧图像
            ip = image_sequence[slice]
            ip_float = ip.astype(np.float32)

            wp = None

            # 估计运动参数
            if transform == TRANSLATION:
                wp = estimate_translation(ip_float, ip_float_ref, ip_pyramid, ip_ref_pyramid, max_iter, tol)
            else:
                wp = estimate_affine(ip_float, ip_float_ref, ip_pyramid, ip_ref_pyramid, max_iter, tol)

            # 应用运动参数变换到当前帧图像
            ip_float_out = np.zeros((width, height), dtype=np.float32)

            if transform == TRANSLATION:
                warp_translation(ip_float_out, ip_float, wp)
            else:
                warp_affine(ip_float_out, ip_float, wp)

            if isinstance(ip, np.ndarray):
                # 如果是彩色图像
                if len(ip.shape) == 3:
                    # 处理彩色图像
                    pass
                else:
                    # 处理灰度图像
                    pass

            # 更新参考图像
            combine(ip_float_ref, ip_float_out)

            # 显示处理进度
            # show_progress(tick / stack_size)

            # 更新 tick
            # tick += 1

